package com.myway.service.ibe.command;

import com.google.common.base.Preconditions;
import com.myway.dto.logs.DTOIBELogs;
import com.myway.service.ibe.config.IBEClientConfig;
import com.travelsky.ebuild.clientapi.axi.POSType;
import com.travelsky.ebuild.clientapi.axi.SourceType;
import com.travelsky.ibe.client.IBEClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * Created by Administrator on 2018/1/15.
 */
@Slf4j
public abstract class BaseIBECommand {

    protected final static String ID_TYPE_NI="NI";
    protected final static String ACTION_CODE_HK="NN";//LL(新Office不能预订)
    /**
     * for open segment...
     */
    protected static final String ACTION_CODE_SA="SA";

    private IBEClientConfig config;

    public IBEClientConfig getConfig() {
        return this.config;
    }

    public void setConfig(IBEClientConfig config) {
        this.config = config;
    }

    /**
     * 连接IBE服务器
     * 1233
     */
    public void configIBEClient(IBEClient client){
        Preconditions.checkNotNull(client,"No ibe instance to set params");
        client.setAgentInfo(config.getOffice(), config.getCustomno(), config.getValidationno());
        client.setAppName(config.getApp());
        String ipAddress=config.getIp();
        log.info("use ip "+ipAddress+" to access travelsky ibe.");
        client.setConnectionInfo("122.119.114.32",Integer.parseInt(config.getPort()));
    }

    /**
     * 设置航空公司配置
     */
    public void configIBEClient(IBEClient client,String airline){
        //not set airline ,use default.
        if(!StringUtils.hasText(airline)){
            return;
        }
        Assert.notNull(client,"No ibe instance to set params");
        client.setAgentInfo(config.getOffice(), config.getCustomno(), config.getValidationno());
        client.setAppName(config.getApp());
        String ipAddress=config.getIp();
        if(new Date().getTime()%2==0){
//            ipAddress=config.getBackupIp();
        }
        log.info("use ip "+ipAddress+"/"+config.getOffice()+" to access travelsky ibe.");
        client.setConnectionInfo(ipAddress,Integer.parseInt(config.getPort()));

    }

    public POSType configIBEClientPrice(IBEClient client){
        configIBEClient(client);
        POSType posType = new POSType("1E",null,"TBS");
        //添加IATA_Number信息
        SourceType source = new SourceType("6","10003195","IATA_Number");
        // source.setChannelID("B");
        posType.addSource(source);

        //添加officecode信息
        SourceType officesource = new SourceType();
        officesource.setPseudoCityCode("TBS319");
        posType.addSource(officesource);

        //添加departmentcode信息
        SourceType deptype = new SourceType("6","CMJ","DepartmentCode");
        posType.addSource(deptype);
        return posType;
    }
    public void configIBEClient(IBEClient client, String airline, DTOIBELogs ibeLog, int international)
    {
        //not set airline ,use default.
        if(!StringUtils.hasText(airline)){
            return;
        }
        Assert.notNull(client,"No ibe instance to set params");
        client.setAgentInfo(config.getOffice(), config.getCustomno(), config.getValidationno());
        client.setAppName(config.getApp());

        String ipAddress=config.getIp();

        if(new Date().getTime()%2==0){
//            ipAddress=config.getBackupIp();
        }
        log.info("use ip "+ipAddress+"/"+config.getOffice()+" to access travelsky ibe.");
        client.setConnectionInfo(ipAddress,Integer.parseInt(config.getPort()));
        String printNo = "" ;
        String prints[];
        try{
            //获取可用打票机号
            if(international==1)
            {
                //国际票
                prints = config.getPrintInterNos().split( "," );
            }
            else
            {
                prints = config.getPrintNos().split( "," );

            }
            if(prints != null && prints.length > 1)
            {
                Date dateNow = new Date();
                long random = dateNow.getTime()%prints.length;
                printNo = prints[Integer.parseInt( String.valueOf( random ) )];
            }
            else if(prints != null &&prints.length==1)
            {
                printNo = prints[0];
            }
        }catch (Exception e) {
            // TODO: handle exception
        }
        //set EtIBELog value
        if(printNo != ""){
            ibeLog.setPrintNo( printNo );
        }
//        ibeLog.setOfficeNo( config.getOffice().toUpperCase() );
//        ibeLog.setAirlineCode( airline );
//        ibeLog.setIbeIp( ipAddress );
    }

    public String test(){
        return "success";
    }
}
